﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class LinkController
    {
        #region value
        private readonly LinkRefBO _linkBo = new LinkRefBO();
        private readonly LinkValueBO _linkValueBo=new LinkValueBO();
        #endregion

        #region Ham lay du lieu

        public IQueryable<RelaxLinkDataContract> GetLinkDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _linkBo.GetLinkDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(List<RelaxLinkDataContract> linkDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var iD = 0;
                var link = new LINK_REF
                {
                    link = linkDataContract[0].Link,
                    target = linkDataContract[0].Target,
                    ord = linkDataContract[0].Ord,
                    active = linkDataContract[0].Active
                };
                _linkBo.Add(link);

                var first = _linkBo.GetLinkDataContracts().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = Convert.ToInt32(first.Id);
                    foreach (var item in linkDataContract)
                    {
                        var linkValue = new LINK_VALUE
                        {
                            linkid = iD,
                            languagecode = item.Languagecode,
                            name = item.Name,
                            keyword = item.Keyword,
                            decription = item.Decription,
                        };
                        _linkValueBo.Add(linkValue);
                    }
                }
                   
                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update

        public Boolean Update(List<RelaxLinkDataContract> linkDataContract)
        {
            using (var scope = new TransactionScope())
            {
                
                var link = new LINK_REF
                {
                    id = Convert.ToInt32(linkDataContract[0].Id),
                    link = linkDataContract[0].Link,
                    target = linkDataContract[0].Target,
                    ord = linkDataContract[0].Ord,
                    active = linkDataContract[0].Active
                };

                _linkBo.Update(link);
                foreach (var item in linkDataContract)
                {
                    var linkValue = new LINK_VALUE
                    {
                        linkid = Convert.ToInt32(item.Id),
                        languagecode = item.Languagecode,
                        name = item.Name,
                        keyword = item.Keyword,
                        decription = item.Decription,
                    };
                    if (!_linkValueBo.Update(linkValue))
                    {
                        _linkValueBo.Add(linkValue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId, long iLanguageCode)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _linkValueBo.Delete(id,iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}
